package thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class MyThreadPool {
    private Thread[] pool;
    private BlockingQueue<Runnable> queue;

    public MyThreadPool(int capacity) {
        queue = new LinkedBlockingQueue<>();
        for (int i = 0; i < capacity; i++) {
            Thread t = new Thread(() -> {
                try {
                    queue.take().run();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            },"thread" + i);
            pool[i] = t;
            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}
